package com.semidata.trp.business.report;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.semidata.rtd.core.log.Logger;
import com.semidata.trp.message.business.Report;

public class MonitorReportData {

	private String server;
	private long time;
	private Map<Integer, Map<Integer, Double>> organizationCosts = new HashMap<>();
	private Map<Integer, Map<Integer, Double>> strategyCosts = new HashMap<>();
	private Map<Integer, Map<Integer, Long>> strategyImpressions = new HashMap<>();
	private Map<Integer, Long> strategyClicks = new HashMap<>();
	private Map<Integer, Long> resourceImpressions = new HashMap<>();
	private Map<Integer, Long> resourceClicks = new HashMap<>();

	private Report.MonitorReport data;
	private Set<KeyWord> keyWords = new HashSet<>();

	public MonitorReportData(byte[] bytes) throws Exception {
		this(Report.MonitorReport.parseFrom(bytes));
	}

	public MonitorReportData(Report.MonitorReport data) throws UnsupportedEncodingException {
		this.data = data;
		this.server = data.getServer();
		this.time = data.getTime();

		for (Report.OrganizationCost item : data.getOrganizationCostList()) {
			int orgId = item.getOrganization();
			if (organizationCosts.get(orgId) == null) {
				organizationCosts.put(orgId, new HashMap<Integer, Double>());
			}
			Map<Integer, Double> balances = organizationCosts.get(orgId);
			balances.put(0, item.getCostIn());
			balances.put(1, item.getCostOut());
			balances.put(2, item.getCostAlly());
			organizationCosts.put(orgId, balances);
			// organizationCosts.put(item.getOrganization(), item.getCost());
		}

		for (Report.StrategyCost item : data.getStrategyCostList()) {
			int straId = item.getStrategy();
			if (strategyCosts.get(straId) == null) {
				strategyCosts.put(straId, new HashMap<Integer, Double>());
			}
			Map<Integer, Double> balances = strategyCosts.get(straId);
			balances.put(0, item.getCostIn());
			balances.put(1, item.getCostOut());
			balances.put(2, item.getCostAlly());
			strategyCosts.put(straId, balances);
			// strategyCosts.put(item.getStrategy(), item.getCost());
		}

		for (Report.StrategyImpression item : data.getStrategyImpressionList()) {
			int straId = item.getStrategy();
			if (strategyImpressions.get(straId) == null) {
				strategyImpressions.put(straId, new HashMap<Integer, Long>());
			}
			Map<Integer, Long> balances = strategyImpressions.get(straId);
			balances.put(0, item.getImpressionIn());
			balances.put(1, item.getImpressionOut());
			balances.put(2, item.getImpressionAlly());
			strategyImpressions.put(straId, balances);
			// strategyImpressions.put(item.getStrategy(),
			// item.getImpression());
		}

		for (Report.StrategyClick item : data.getStrategyClickList()) {
			strategyClicks.put(item.getStrategy(), item.getClick());
		}

		for (Report.ResourceImpression item : data.getResourceImpressionList()) {
			resourceImpressions.put(item.getResource(), item.getImpression());
		}

		for (Report.ResourceClick item : data.getResourceClickList()) {
			resourceClicks.put(item.getResource(), item.getClick());
		}
		for (Report.KeyWordList keyword : data.getKeywordList()) {
			if (keyword.getKeyword1().length() == 0)
				continue;
			else {
				KeyWord kw = new KeyWord();
				kw.setKey1(keyword.getKeyword1());
				if (!keyword.getKeyword2().isEmpty()) {
					kw.setKey2(keyword.getKeyword2());
					if (!keyword.getKeyword3().isEmpty())
						kw.setKey3(keyword.getKeyword3());
				}
				keyWords.add(kw);
			}
		}
	}

	public String getServer() {
		return server;
	}

	public long getTime() {
		return time;
	}

	public Map<Integer, Map<Integer, Double>> getOrganizationCosts() {
		return organizationCosts;
	}

	public Map<Integer, Map<Integer, Double>> getStrategyCosts() {
		return strategyCosts;
	}

	public Map<Integer, Map<Integer, Long>> getStrategyImpressions() {
		return strategyImpressions;
	}

	public Map<Integer, Long> getStrategyClicks() {
		return strategyClicks;
	}

	public Map<Integer, Long> getResourceImpressions() {
		return resourceImpressions;
	}

	public Map<Integer, Long> getResourceClicks() {
		return resourceClicks;
	}

	public Report.MonitorReport getData() {
		return data;
	}

	public Set<KeyWord> getKeyWords() {
		return keyWords;
	}

}
